﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace NB_Controller_Test
{
    class DevAliasNames
    {
        public class DevAliasName
        {
            public string strMac;
            public string strName;
            public string strDetail="";
        }
        //名字不能有空格
        protected const string XML_NODE_PROJECT = "Project";

        //发送命令相关定义
        protected const string XML_DEVICE = "Device";
        protected const string XML_MAC = "Mac";
        protected const string XML_NAME = "Name";        
        protected const string XML_DETAIL = "Detail";

        public string PathName;

        public DevAliasName[] devAliasNameArry = new DevAliasName[100];
        public UInt16 uiCount = 0;

        public DevAliasNames()
        {
            PathName = System.Windows.Forms.Application.StartupPath + "\\DevAliasNameConfig.xml";
            Open();
        }

        public bool Open()
        {
            bool bRet = true;

            try
            {
                XmlDocument _xmldoc = new XmlDocument();
                _xmldoc.Load(PathName);

                XmlNode nodePage = _xmldoc.SelectSingleNode(XML_NODE_PROJECT);
                if (nodePage != null)
                {
                    foreach (XmlNode _node in nodePage.ChildNodes)
                    {
                        if (_node.NodeType != XmlNodeType.Element)
                            continue;

                        XmlElement _elm = (XmlElement)_node;

                        //================================
                        if (_elm.Name.Equals(XML_DEVICE))
                        {
                            ParseXML(_elm);
                        }
                    }
                }
            }
            catch
            {
                //Save();
            }
            return bRet;
        }

        public bool Save()
        {
            if (File.Exists(PathName))
            {
                File.Delete(PathName);
            }

            XDocument xDoc = new XDocument();
            XElement xRoot = new XElement(XML_NODE_PROJECT);
            // 添加节点使用Add
            xDoc.Add(xRoot);

            for (int i = 0; i < uiCount; i++)
            {
                DevAliasNames.DevAliasName sendCmd = devAliasNameArry[i];

                XElement xDevItem = new XElement(XML_DEVICE);
                xRoot.Add(xDevItem);

                XElement xDevMacItem = new XElement(XML_MAC);
                xDevMacItem.Value = sendCmd.strMac;
                xDevItem.Add(xDevMacItem);

                XElement xDevNameItem = new XElement(XML_NAME);
                xDevNameItem.Value = sendCmd.strName;
                xDevItem.Add(xDevNameItem);

                XElement xDevDetailItem = new XElement(XML_DETAIL);
                xDevDetailItem.Value = sendCmd.strDetail;
                xDevItem.Add(xDevDetailItem);                
            }
            
            //保存该文档  
            xDoc.Save(PathName);

            return true;
        }

        public bool AddNewDevAliasName(DevAliasName sendCmd)
        {
            if (uiCount >= 100/*Marshal.SizeOf(sendCmdArry)*/)
                return false;
            else
                devAliasNameArry[uiCount++] = sendCmd;

            return true;
        }

        ///*******************************************************************************
        /// <summary>
        /// 解析 近端秘钥列表
        /// </summary>
        /// <param name="_elm"></param>
        ///*******************************************************************************
        public void ParseXML(XmlElement _elm)
        {
            if (_elm.Name.Equals(XML_DEVICE))
            {
                DevAliasName sendCmd = new DevAliasName();

                foreach (XmlNode xmlNode in _elm.ChildNodes)
                {
                    if (xmlNode.NodeType != XmlNodeType.Element)
                        continue;
                    XmlElement xmlElm = (XmlElement)xmlNode;

                    if (xmlElm.Name.Equals(XML_MAC))
                    {
                        sendCmd.strMac = xmlElm.InnerText;
                    }
                    else if (xmlElm.Name.Equals(XML_NAME))
                    {
                        sendCmd.strName = xmlElm.InnerText;
                    }
                    else if (xmlElm.Name.Equals(XML_DETAIL))
                    {
                        sendCmd.strDetail = xmlElm.InnerText;
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("xmlElm.Name : " + xmlElm.Name + " is not save!");
                    }
                }

                AddNewDevAliasName(sendCmd);
            }
        }

        /// <summary>
        /// 根据名称查找发送的数据
        /// </summary>
        /// <param name="strMacInfo"></param>
        /// <returns></returns>
        public DevAliasNames.DevAliasName FindDevAliasNameByMac(string strMacInfo)
        {
            for (int i = 0; i < uiCount; i++)
            {
                DevAliasNames.DevAliasName sendCmd = devAliasNameArry[i];
                if (sendCmd.strMac == strMacInfo)
                {
                    return sendCmd;
                }
            }

            //没有找到，主动新增一个并设置别名为Mac信息
            DevAliasName devAlias = new DevAliasName();
            devAlias.strMac = strMacInfo;
            devAlias.strName = strMacInfo;
            AddNewDevAliasName(devAlias);

            return devAlias;
        }

        public void UpdateAliasName(string strMacInfo, string strAliasName)
        {
            for (int i = 0; i < uiCount; i++)
            {
                DevAliasNames.DevAliasName sendCmd = devAliasNameArry[i];
                if (sendCmd.strMac == strMacInfo)
                {
                    sendCmd.strName = strAliasName;
                    return;
                }
            }

            //没有找到，主动新增一个并设置别名为Mac信息
            DevAliasName devAlias = new DevAliasName();
            devAlias.strMac = strMacInfo;
            devAlias.strName = strMacInfo;
            AddNewDevAliasName(devAlias);
        }
    }
}
